module.exports = function initResponseParameter(apiTestComp) {
	let {
		responseParameterComp,
	} = apiTestComp
	responseParameterComp.treegrid({
		title: '响应参数',
		fit: true,
		fitColumns: true,
		treeField: 'name',
		lines: true,
		idField: '_treeId_',
		dblclickToEdit: false,
		clickToEdit: false,
		columns: [
			[{
				field: 'name',
				title: '变量名',
				width: 60,
			}, {
				field: 'content',
				title: '含义',
				width: 60,

			}, {
				field: 'type',
				title: '类型',
				width: 30,

			}, {
				field: 'remark',
				title: '备注',
				width: 50,
				/**
				 * 检查每个返回参数
				 * 1. 参数是否存在
				 * 2. 参数类型是否正确
				 * @param  {[type]} value [description]
				 * @param  {[type]} row   [description]
				 * @return {[type]}       [description]
				 */
				formatter(value, row) {
					let tmpResult = apiTestComp.store.tmpResult
					let tmpMap = apiTestComp.store.tmpResponseParameterMap
					tmpMap[row._treeId_] = row
					let result = checkRow(row, tmpResult, tmpMap)
					return `<span style="${result.style}"><img src="/easyui/themes/icons/${result.icon}.png">&nbsp;${result.info}</span>`
				},
			}]
		],
	});
}

function checkRow(row, tmpResult, tmpMap) {
	let result = {
		icon: 'ok',
		info: '参数正确！',
		style: '',
	}
	let keyArr = []
	let tmpRow = row
	while (tmpRow._parentId) {
		tmpRow = tmpMap[tmpRow._parentId]
		keyArr.push(tmpRow.name)
	}
	for (let i = keyArr.length - 1; i > -1; i--) {
		tmpResult = tmpResult[keyArr[i]]
	}
	if (Array.isArray(tmpResult))
		tmpResult = tmpResult[0]
	let resultValue = tmpResult[row.name]
	if (resultValue === undefined) {
		result.icon = 'cancel'
		result.style = 'color:red;'
		result.info = '参数不存在！'
	} else if ($._ulType(resultValue) != row.type) {
		result.icon = 'cancel'
		result.style = 'color:red;'
		result.info = `值类型错误！(${$._ulType(resultValue)})`
	}
	return result
}